import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:riverpod_t/base/refresh_header.dart' show RefreshHeader;

import 'loadiing_footer.dart';

/// 上拉下拉 直接传入listview 即可

class CommonRefresher extends StatelessWidget {
  final bool enablePullUp;
  final bool enablePullDown;
  final Function() onRefresh;
  final Function() onLoading;
  final RefreshController refreshController;
  final Color? bgColor;
  final Widget listWidget;
  const CommonRefresher({
    super.key,
    required this.enablePullUp,
    required this.enablePullDown,
    required this.onRefresh,
    required this.onLoading,
    required this.refreshController,
    this.bgColor,
    required this.listWidget,
  });

  /// 刷新组件初始化配置
  ///
  /// [list] 列表组件
  Widget _configuration(Widget list) {
    return RefreshConfiguration(
      headerTriggerDistance: 10,
      footerTriggerDistance: 0,
      enableBallisticLoad: false,
      headerBuilder: () => const RefreshHeader(),
      footerBuilder: () => const LoadingFooter(),
      child: Container(
        color: bgColor,
        child: SmartRefresher(
          physics: const BouncingScrollPhysics(),
          enablePullUp: enablePullUp,
          enablePullDown: enablePullDown,
          controller: refreshController,
          onRefresh: onRefresh,
          onLoading: onLoading,
          child: list,
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) => _configuration(listWidget);
}
